package com.wjf.shortlink.shortlink.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wjf.shortlink.admin.common.convention.result.Result;
import com.wjf.shortlink.admin.common.convention.result.Results;
import com.wjf.shortlink.shortlink.dto.req.ShortLinkBatchCreateReqDTO;
import com.wjf.shortlink.shortlink.dto.req.ShortLinkCreateReqDTO;
import com.wjf.shortlink.shortlink.dto.req.ShortLinkPageReqDTO;
import com.wjf.shortlink.shortlink.dto.req.ShortLinkUpdateReqDTO;
import com.wjf.shortlink.shortlink.dto.resp.ShortLinkBatchCreateRespDTO;
import com.wjf.shortlink.shortlink.dto.resp.ShortLinkCreateRespDTO;
import com.wjf.shortlink.shortlink.dto.resp.ShortLinkGroupCountQueryRespDTO;
import com.wjf.shortlink.shortlink.dto.resp.ShortLinkPageRespDTO;
import com.wjf.shortlink.shortlink.service.ShortLinkService;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 短链接控制层
@RestController
@RequiredArgsConstructor
public class ShortLinkController {

    private final ShortLinkService shortLinkService;

    // 短链接跳转原始链接
    @GetMapping("/{short-uri}")
    public void restoreUrl(@PathVariable("short-uri") String shortUri, ServletRequest request, ServletResponse response) {
        shortLinkService.restoreUrl(shortUri, request, response);
    }

    //创建短链接
    @PostMapping("/api/short-link/v1/create")
    public Result<ShortLinkCreateRespDTO> createShortLink(@RequestBody ShortLinkCreateReqDTO shortLinkCreateReqDTO) {
        return Results.success(shortLinkService.createShortLink(shortLinkCreateReqDTO));
    }

    //通过分布式锁创建短链接
    @PostMapping("/api/short-link/v1/create/by-lock")
    public Result<ShortLinkCreateRespDTO> createShortLinkByLock(@RequestBody ShortLinkCreateReqDTO shortLinkCreateReqDTO) {
        return Results.success(shortLinkService.createShortLinkByLock(shortLinkCreateReqDTO));
    }

    //批量创建短链接
    @PostMapping("/api/short-link/v1/create/batch")
    public Result<ShortLinkBatchCreateRespDTO> batchCreateShortLink(@RequestBody ShortLinkBatchCreateReqDTO shortLinkBatchCreateReqDTO) {
        return Results.success(shortLinkService.batchCreateShortLink(shortLinkBatchCreateReqDTO));
    }

    // 统计各分组中短链接数量
    @GetMapping("api/short-link/v1/count")
    public Result<List<ShortLinkGroupCountQueryRespDTO>> ListGroupShortLinkCount(@RequestParam("gIds") List<String> gIds) {
        return Results.success(shortLinkService.ListGroupShortLinkCount(gIds));
    }

    //分页查询短链接
    @GetMapping("/api/short-link/v1/page")
    public Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO shortLinkPageReqDTO) {
        return Results.success(shortLinkService.pageShortLink(shortLinkPageReqDTO));
    }

    //修改短链接
    @PostMapping("api/short-link/v1/update")
    public Result<Void> updateShortLink(@RequestBody ShortLinkUpdateReqDTO shortLinkUpdateReqDTO) {
        shortLinkService.updateShortLink(shortLinkUpdateReqDTO);
        return Results.success();
    }
}
